﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace iLapKan
{
    public static class StringLP
    {
        public static string Replace1time(string val, string oldstr, string newstr)
        {
            if (val.IndexOf(oldstr) >= 0)
            {
                string buf = val.Substring(0, val.IndexOf(oldstr));
                string buf1 = val.Substring(val.IndexOf(oldstr) + oldstr.Length, val.Length - (val.IndexOf(oldstr) + oldstr.Length));
                return buf + newstr + buf1;
            }
            else return val;
        }

        public static string filterStr(string var, string filstr)
        {
            string rs = "";
            for (int i = 0; i < var.Length; i++)
                if (filstr.IndexOf(var.Substring(i, 1)) >= 0)
                    rs += var.Substring(i, 1);
            return rs;
        }

        public static string[] SplitByAny(this string str, string[] separaters)
        {
            string[] items = str.Replace(separaters[0], (char)(128) + "").Split((char)(128));
            foreach (string separater in separaters)
                if (str.IndexOf(separater) >= 0)
                    items = str.Replace(separater, (char)(128) + "").Split((char)(128));

            return items;
        }

        public static string[] SplitByAny(this string str, string separaters)
        {
            string[] items = str.Replace(separaters.Substring(0, 1), (char)(128) + "").Split((char)(128));
            for (int i = 0; i < separaters.Length; i++)
            {
                string separater = separaters.Substring(i, 1);
                if (str.IndexOf(separater) >= 0)
                    items = str.Replace(separater, (char)(128) + "").Split((char)(128));
            }

            return items;
        }

        public static string[] splitByLine(this string content)
        {
            return StringLP.SplitByAny(content, new string[] { "\r\n" });
        }

        public static string[] splitByTab(this string content)
        {
            return StringLP.SplitByAny(content, new string[] { "\t" });
        }

        public static string getString(object o)
        {
            if (o == null) return "";
            return o.ToString();
        }

        public static string getGioiTinh(object o)
        {
            if (o is bool)
            {
                if ((bool)o) return "Nam";
                else
                    return "Nu";
            }

            if (o is String)
            {
                String gioiTinh = (String)o;
                if (gioiTinh.ToLower() == "nam") return "Nam";
                else return "Nu";
            }

            return "";
        }
    }
}
